package cn.fr4nk.crawler.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CsvUtil {
	private static Log log = LogFactory.getLog(CsvUtil.class);

	public static void main(String[] args) {
		String[] arr  = new String[] {"1111", "\"222,222\"", "333"};
		List<String[]> content = new ArrayList<String[]>();
		content.add(arr);
		String path = "E:\\tmp\\frank.txt";
		
		write(path, content);
		
		List<String[]> ll = read(path);
		for (String[] ss: ll) {
			System.out.println(ss);
		}
	}

	

	public static void write(String fileName, String content) {
		File file = new File(fileName);
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(file));
			writer.write(content);

		} catch (Exception e) {
			log.warn("write file error: " + fileName);
			log.warn(e);
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}
	}
	

	public static void write(String fileName, List<String[]> content) {
		File file = new File(fileName);
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(file));
			for (String[] items : content) {
				if (items != null && items.length > 0) {
					StringBuilder sb = new StringBuilder();
					for (String s : items) {
						sb.append(s);
						sb.append(',');
					}
					sb.deleteCharAt(sb.length()-1);
					sb.append('\n');
					writer.write(sb.toString());
				}
			}

		} catch (Exception e) {
			log.warn("write csv error: " + fileName);
			log.warn(e);
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}

	}
	
	public static String readString(String fileName) {

		BufferedReader reader = null;
		StringBuilder sb = new StringBuilder();
		String line = "";
		try {
			reader = new BufferedReader(new FileReader(fileName));
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (FileNotFoundException e) {
			log.warn("读取csv失败，文件不存在");
		} catch (IOException e) {
			log.warn("读取csv失败，读取一行数据失败");
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}
		return sb.toString();
	}
	

	public static List<String[]> read(String fileName) {

		List<String[]> res = null;
		String cvsSplitBy = ",";
		String line = "";
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(fileName));
			res = new ArrayList<String[]>();
			while ((line = reader.readLine()) != null) {
				res.add(line.split(cvsSplitBy));
			}
		} catch (FileNotFoundException e) {
			log.warn("读取csv失败，文件不存在");
		} catch (IOException e) {
			log.warn("读取csv失败，读取一行数据失败");
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}

		return res;
	}

}
